import os

class ExportIdx:
    m_day_export_idx_data = 0
    m_day_export_idx_start_price = 1
    m_day_export_idx_max_price = 2
    m_day_export_idx_min_price = 3
    m_day_export_idx_end_price =4
    m_day_export_idx_exchange_num = 5
    m_day_export_idx_exchange_money = 6

class OneStockInfo:
    def __init__(self,stock_id):
        # stock id (600004)
        self.m_id = stock_id
        # list of one_day_info(2018/05/02,15.23,15.43,15.05,15.39,8094412,123385176.00)
        self.m_mul_day_info = []
        return
        
    def append_one_day_info(self,str_one_day_info):
        self.m_mul_day_info.append(str_one_day_info.split(','))
        return

class stockInfoMgr:
    def __init__(self):
        # id --> OneStockInfo
        self.m_stocks_map = {}
        return

    def append_one_day_stock_info(self,stock_id,str_one_day_info):
        # print "append %s --> %s"%(stock_id,str_one_day_info)
        if not self.m_stocks_map.has_key(stock_id):
            self.m_stocks_map[stock_id] = OneStockInfo(stock_id)

        self.m_stocks_map[stock_id].append_one_day_info(str_one_day_info)
        return

class stockUPFilter:
    def isStockUP(self,OneStockInfo,days):
        idx = 0
        for one_day_info in OneStockInfo.m_mul_day_info:
            if idx <= (len(OneStockInfo.m_mul_day_info)-days):
                idx=idx+1
                continue
            if not self.isStockOneDayUP(one_day_info):
                return False
        return True

    def isStockOneDayUP(self,one_day_info):
        return float(one_day_info[ExportIdx.m_day_export_idx_end_price]) > \
            float(one_day_info[ExportIdx.m_day_export_idx_start_price])

class stockFilter:
    def __init__(self):
        return

    def isStockOK(self,OneStockInfo):
        return self.isStockUP(OneStockInfo)
    
    def isStockUP(self,OneStockInfo):
        fl = stockUPFilter()
        # up for 5 days
        return fl.isStockUP(OneStockInfo,5)    

class dataInfoRead:
    def __init__(self,data_dir):
        self.m_data_dir = data_dir
        self.m_stock_info_mgr = stockInfoMgr()
        return

    def is_info_line(self,one_line):
        return one_line.find("2018") == 0

    def read_one_file(self,dir_name,file_name):
        # SH#600004.txt
        #self.m_dictFileInfo[os.path.join(dirname, f)] = os.path.getmtime(os.path.join(dirname, f))
        stock_id = file_name.split('#')[1].split('.')[0]
        
        one_stock_file = open(os.path.join(dir_name, file_name))
        while True:
            one_line = one_stock_file.readline()
            if len(one_line) == 0:
                break
            if one_line[len(one_line)-1] == '\n':
                one_line = one_line[0:len(one_line)-1]
            if self.is_info_line(one_line):
                self.m_stock_info_mgr.append_one_day_stock_info(stock_id,one_line)
        return

    def read_data(self):
        for (dirname, subdir, subfile) in os.walk(self.m_data_dir):
			for f in subfile:
				self.read_one_file(dirname,f)

    def recordOKStock(self):
        fp = open("OK_stock.txt","w")
        fl = stockFilter()
        for one_stock_id in self.m_stock_info_mgr.m_stocks_map:
            if fl.isStockOK(self.m_stock_info_mgr.m_stocks_map[one_stock_id]):
                fp.write(one_stock_id+"\n")
        fp.close()

def main():
    data_info = dataInfoRead("data")
    data_info.read_data()

    data_info.recordOKStock()


main()